package com.tang.calculator.utils;

import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import org.springframework.core.io.ClassPathResource;

import java.io.*;
import java.util.List;

/**
 * @auther: zhiy
 * @date: 2021/3/17
 * @description:
 */
@Slf4j
public class JsonUtils {

    /**
     * 读取json文件并且转换成字符串
     * @param pactFile 文件的路径
     * @return
     * @throws IOException
     */
    public static List readJsonData(String pactFile, Class clazz) {
        // 读取文件数据
        ClassPathResource resource = new ClassPathResource(pactFile);
        StringBuffer strbuffer = new StringBuffer();
        try {
            InputStreamReader inputStreamReader = new InputStreamReader(resource.getInputStream(), "UTF-8");
            BufferedReader in  = new BufferedReader(inputStreamReader);

            String str;
            while ((str = in.readLine()) != null) {
                strbuffer.append(str);
            }
            in.close();
        } catch (IOException e) {
            log.error(e.getMessage());
        }
        return JSON.parseArray(strbuffer.toString(), clazz);
    }


}
